<?php
/**
 * Created by PhpStorm.
 * User: mars
 * Date: 2018/9/3
 * Time: 下午4:39
 */

namespace api\models;


use yii\base\Model;
use yii\web\UploadedFile;

class ImgUpload extends Model
{

    /**
     * @var UploadedFile
     */
    public $file;

    public function rules()
    {
        return [
            [['file'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg'],
        ];
    }

    public function upload()
    {
        if ($this->validate()) {

            $new_name = \Yii::getAlias('@avatar') . $this->file->baseName . '-' . time() . '.' . $this->file->extension;

            $this->file->saveAs($new_name);

            return $new_name;

        } else {
            return false;
        }
    }


}
